package com.itheima.exception;

import com.itheima.common.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R getSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e){
        e.printStackTrace();
        String s = e.getMessage();
        if (s.contains("Duplicate entry")){
            return R.error(s.split("")[2]+",用户名已存在！");
        }
        return R.error("程序开小差了!");
    }
    @ExceptionHandler(CustomException.class)
    public R getCustomException(CustomException e){
        e.printStackTrace();
        return R.error(e.getMessage());
    }
}
